#ifndef _OTA_FUNC_H_
#define _OTA_FUNC_H_
#include "FSMC_SRAM.h"
#include "stdint.h"

#define OTA_DEBUG 1

extern uint8_t *read_from_spi_flash_buffer;

#pragma pack(1)
typedef struct
{
  uint32_t request_id;

  char fw_checksum[65];
  char fw_title[32];
  char fw_checksum_algorithm[16];
  char fw_version[16];

  uint32_t fw_size;
  uint16_t current_package_index;
  uint32_t left_fw_size;

  uint8_t reserved_common[34]; /* 常用保留，可预留给后续字段 */
  uint8_t reserved_ext[79];    /* 111 - 32 = 79，纯填充 */
} OTA_PROCESS_INFO;
#pragma pack()

extern OTA_PROCESS_INFO *ota_process_info;

void publish_firmware_info_request(void);
int parse_firmware_attributes(const char *payload, OTA_PROCESS_INFO *info);
int mqtt_message_callback(const char *topic, const uint8_t *msg, uint16_t msg_len);
int get_firmware(uint32_t request_id,
                 uint16_t current_package_index,
                 uint32_t chunk_size);
int extract_ids_from_topic(const char *topic,
                           uint32_t *request_id,
                           uint32_t *chunk_index);

int send_to_server_ota_status(char *fw_title, char *fw_version, char *status);
int save_bin_to_target_store(uint32_t address, uint8_t *data, uint32_t size);
int SPIFlash_SHA256_Stream(uint32_t addr, uint32_t total_len, uint8_t *buf, uint8_t hash[32]);

#endif // !_OTA_FUNC_H_
